import axios from 'axios'
import router from '../router/index'
import { Message } from 'element-ui'
axios.defaults.timeout = 30000
// let loadingInstance
axios.interceptors.request.use(config => {
    // loadingInstance = Loading.service({ fullscreen: true })
    config.headers.Authentication = JSON.parse(localStorage.getItem('USER_TOKEN'))
    return config
}, error => {
    // loadingInstance.close()
    Message.error('加载超时')
    return Promise.reject(error)
})
axios.interceptors.response.use(data => {
    // loadingInstance.close()
    if (data.status === 200) {
        let code = data.data.errorCode
        if (code === 'PORTAL-10004') {
            Message({
                message: '很抱歉，登录已过期，请重新登录',
                type: 'error',
                onClose: () => {
                    router.push('/login')
                }
            })
        } else if (code === 'PORTAL-10007') {
            Message({
                message: '很抱歉，该项目被删除或没有权限，请重新进入',
                type: 'error',
                onClose: () => {
                    router.push('/login')
                }
            })
        } else {
            return data
        }
    }
}, error => {
    // loadingInstance.close()
    switch (error.response.status) {
        case 403:
            router.push('/403')
            break
        case 404:
            router.push('/404')
            break
        case 500:
            router.push('/500')
            break
        default:
            router.push('/404')
            break
    }
    return Promise.reject(error)
})
export default axios
